package com.api.sw.duban.action;

import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.sw.duban.model.Assigned;
import com.api.sw.duban.service.AssignedBaseService;
import com.api.sw.duban.util.BaseUtil;
import com.api.sw.duban.util.DubUtil;
import com.api.sw.duban.util.LogTool;
import com.api.sw.duban.util.SqlUtil;
import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.*;
import com.cloudstore.eccom.result.WeaResultMsg;
import weaver.conn.RecordSet;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;

import java.text.SimpleDateFormat;
import java.util.*;

/*
    立项已办结
 */
public class DubanyYbAction {
    private final static LogTool log = new LogTool("/log/duban/",false);
    public DubanyYbAction(){

    };
    //查询条件  文号 标题 编号
    public Map<String, Object> getYbTablelist(Map<String,Object> params, User user) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            if(params!=null){
                log.writeLog(params.toString());
            }

            String wh=(String)Util.null2String(params.get("wh")) ;//文号
            String bt=(String)Util.null2String(params.get("bt")) ;//辩题
            String bh=(String)Util.null2String(params.get("bh")) ;//编号
            String zbdw=(String)Util.null2String(params.get("zbdw")) ;//主办单位

            //日期
            String fksx=(String)Util.null2String(params.get("fksx")) ;//反馈时限
            String fksxfrom=(String)Util.null2String(params.get("fksxfrom")) ;//反馈时限from
            String fksxto=(String)Util.null2String(params.get("fksxto")) ;//反馈时限to

            String lxsx=(String)Util.null2String(params.get("lxsx")) ;//立项时限
            String lxsxfrom=(String)Util.null2String(params.get("lxsxfrom")) ;//立项from
            String lxsxto=(String)Util.null2String(params.get("lxsxto")) ;//反馈时限to

            //1 今天:2本周:3本月:4本季度:5本年:6时间区间:7上年:8:上个月

            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            RecordSet re=new RecordSet();
            String DBType=re.getDBType();
            //log.writeLog("DBType"+DBType);
            String pageID = "17ecc14c-9d64-4a43-81a4-7b7c6f7928571113";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());

            String sqlwhere =" 1=1 ";

            if("sqlserver".equals(DBType)){
                sqlwhere=" 1=1  and a.id in (select ssrwid from uf_dbjblb b where (','+cast(b.djr as varchar(4000))+',' like '%,"+user.getUID()+",%' or b.dbry='"+user.getUID()+"') and isnull(b.zt,-1) in (2)) ";
            }else{
                sqlwhere=" 1=1   and a.id in (select ssrwid from uf_dbjblb b where (','||to_char(substr(b.djr,1,1000))||',' like '%,"+user.getUID()+",%' or b.dbry='"+user.getUID()+"') and isnull(b.zt,-1) in (2)) ";
            }

            if(!"".equals(wh)){
                sqlwhere=sqlwhere+" and a.wh like '%"+wh+"%'";
            }
            if(!"".equals(bt)){
                sqlwhere=sqlwhere+" and a.bt like '%"+bt+"%'";
            }
            if(!"".equals(bh)){
                sqlwhere=sqlwhere+" and a.bh like '%"+bh+"%'";
            }
            if(!"".equals(zbdw)){
                if(zbdw.endsWith(",")){
                    zbdw=zbdw.substring(0,zbdw.length()-1);
                }
                //uf_dcsxzb where substr(jbsj,0,10)>'2020-10-13'
                if("sqlserver".equals(DBType)){
                    sqlwhere=" and ','+cast(a.zbdw as varchar(4000))+',' like '%,"+zbdw+",%'  ";
                }else{
                    sqlwhere=" and ','||to_char(substr(zbdw,1,1000))||',' like '%,"+zbdw+",%'  ";
                }
            }
            //substr(xsfk,0,10)
            if("6".equals(fksx)){
                if("sqlserver".equals(DBType)){
                    //sqlwhere=" and ','+cast(a.zbdw as varchar(4000))+',' like '%,"+zbdw+",%'  ";
                }else{
                    if(!"".equals(fksxfrom)&&!"".equals(fksxto)){
                        sqlwhere=sqlwhere+" substr(xsfk,0,10)>'"+fksxfrom+"' and substr(xsfk,0,10)<'"+fksxto+"'";
                    }else{
                        if(!"".equals(fksxfrom)){
                            sqlwhere=sqlwhere+" substr(xsfk,0,10)>'"+fksxfrom+"'";
                        }
                        if(!"".equals(fksxto)){
                            sqlwhere=sqlwhere+" substr(xsfk,0,10)<'"+fksxto+"'";
                        }
                    }

                }
            }else{
                if("sqlserver".equals(DBType)){
                    sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(xsfk,0,10)");
                }else{
                    sqlwhere=sqlwhere+DubUtil.getRQfW(fksx,"substr(xsfk,0,10)");
                }
            }

            if("6".equals(lxsx)){
                if("sqlserver".equals(DBType)){
                    //sqlwhere=" and ','+cast(a.zbdw as varchar(4000))+',' like '%,"+zbdw+",%'  ";
                }else{
                    if(!"".equals(fksxfrom)&&!"".equals(fksxto)){
                        sqlwhere=sqlwhere+" substr(modedatacreatedate,0,10)>='"+lxsxfrom+"' and substr(modedatacreatedate,0,10)<='"+lxsxto+"'";
                    }else{
                        if(!"".equals(lxsxfrom)){
                            sqlwhere=sqlwhere+" substr(modedatacreatedate,0,10)>'"+lxsxfrom+"'";
                        }
                        if(!"".equals(lxsxto)){
                            sqlwhere=sqlwhere+" substr(modedatacreatedate,0,10)<'"+lxsxto+"'";
                        }
                    }

                }
            }else{
                if("sqlserver".equals(DBType)){
                    sqlwhere=sqlwhere+DubUtil.getRQfW(lxsx,"substr(modedatacreatedate,0,10)");
                }else{
                    sqlwhere=sqlwhere+DubUtil.getRQfW(lxsx,"substr(modedatacreatedate,0,10)");
                }
            }


            log.writeLog(sqlwhere);
            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = " a.*,'uf_dcsxzb' tablename,'sxlb' sxlbfield,'sxlbzl' sxlbzlfield,'hj' hjfield,a.xsjs xsjszt,id titileid ";
            table.setBackfields(fileds);

            table.setSqlform(" uf_dcsxzb a");
            table.setSqlwhere(sqlwhere);
            table.setSqlorderby("id");
            table.setSqlprimarykey("id");
            table.setSqlisdistinct("false");

            table.getColumns().add(new WeaTableColumn("id").setDisplay(WeaBoolAttr.FALSE));   //设置为不显
            table.getColumns().add(new WeaTableColumn("30%", "事项标题", "titileid","titileid","com.api.sw.duban.util.dubanTran.getDubanTitle"));
            table.getColumns().add(new WeaTableColumn("20%", "主办单位", "zbdw","zbdw","com.api.sw.duban.util.dubanTran.getSwDpartmentnameByid"));
            WeaTableColumn sxlb=new WeaTableColumn("20%", "事项类别", "sxlb","sxlb","com.api.sw.duban.util.dubanTran.getSelectnameByValue");
            sxlb.setOtherpara("column:tablename").setOtherpara2("column:sxlbfield");
            table.getColumns().add(sxlb);
            //WeaTableColumn sxlbzl=new WeaTableColumn("20%", "子类", "sxlbzl","sxlbzl","com.api.sw.duban.util.dubanTran.getSelectnameByValue");
            //sxlbzl.setOtherpara("column:tablename").setOtherpara2("column:sxlbzlfield");
            //table.getColumns().add(sxlbzl);
            table.getColumns().add(new WeaTableColumn("10%", "限时签收", "xsjs","xsjs"));

            WeaTableColumn hj=new WeaTableColumn("20%", "缓急", "hj","hj","com.api.sw.duban.util.dubanTran.getSelectnameByValue");
            hj.setOtherpara("column:tablename").setOtherpara2("column:hjfield");
            table.getColumns().add(hj);
            table.getColumns().add(new WeaTableColumn("10%", "交办时间", "jbsj","jbsj"));
            table.getColumns().add(new WeaTableColumn("10%", "办结时间", "bjsj","bjsj","com.api.sw.duban.util.dubanTran.getBjTime").setOtherpara("column:id"));//取离最近的时间

            //增加右侧操作选项
            //设置check是否可用
            List<WeaTableCheckboxpopedom> checkboxpopedomList = new ArrayList<>();
            WeaTableCheckboxpopedom checkboxpopedom = new WeaTableCheckboxpopedom();
            checkboxpopedom.setPopedompara("column:id");
            checkboxpopedomList.add(checkboxpopedom);

            table.setCheckboxList(checkboxpopedomList);


            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }
    public Map<String, Object> getCondition(Map<String,Object> params, User user) {

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //角色判断参考代码
//        if(!HrmUserVarify.checkUserRight("LogView:View", user)){
//            apidatas.put("hasRight", false);
//            return apidatas;
//        }

        if (null == user){
            apidatas.put("hasRight", false);
            return apidatas;
        }

        apidatas.put("hasRight", true);

        /**
         * 高级搜索条件
         */
        ConditionFactory conditionFactory = new ConditionFactory(user);

        //条件组
        List<SearchConditionGroup> addGroups = new ArrayList<SearchConditionGroup>();

        List<SearchConditionItem> conditionItems = new ArrayList<SearchConditionItem>();

        //文本输入框
        SearchConditionItem wh = conditionFactory.createCondition(ConditionType.INPUT,502327, "wh");
        wh.setColSpan(2);//定义一行显示条件数，默认值为2,当值为1时标识该条件单独占一行
        wh.setFieldcol(16);	//条件输入框所占宽度，默认值18
        wh.setLabelcol(8);
        wh.setViewAttr(2);  //	 编辑权限  1：只读，2：可编辑， 3：必填   默认2
        wh.setLabel("文号"); //设置文本值  这个将覆盖多语言标签的值
        conditionItems.add(wh);


        //文本输入框
        SearchConditionItem bh = conditionFactory.createCondition(ConditionType.INPUT,502327, "bh");
        bh.setColSpan(2);//定义一行显示条件数，默认值为2,当值为1时标识该条件单独占一行
        bh.setFieldcol(16);	//条件输入框所占宽度，默认值18
        bh.setLabelcol(8);
        bh.setViewAttr(2);  //	 编辑权限  1：只读，2：可编辑， 3：必填   默认2
        bh.setLabel("编号"); //设置文本值  这个将覆盖多语言标签的值
        conditionItems.add(bh);

        //文本输入框
        SearchConditionItem bt = conditionFactory.createCondition(ConditionType.INPUT,502327, "bt");
        bt.setColSpan(2);//定义一行显示条件数，默认值为2,当值为1时标识该条件单独占一行
        bt.setFieldcol(16);	//条件输入框所占宽度，默认值18
        bt.setLabelcol(8);
        bt.setViewAttr(2);  //	 编辑权限  1：只读，2：可编辑， 3：必填   默认2
        bt.setLabel("标题"); //设置文本值  这个将覆盖多语言标签的值
        conditionItems.add(bt);

        //主办单位
        SearchConditionItem zbdw = conditionFactory.createCondition(ConditionType.BROWSER,502327, "zbdw","142");
        zbdw.setLabel("主办单位");
        conditionItems.add(zbdw);
        //反馈时限
        SearchConditionItem fksx = conditionFactory.createCondition(ConditionType.DATE,502327, new String[]{"fksx", "fksxfrom", "fksxto"});
        fksx.setLabel("反馈时限");
        conditionItems.add(fksx);
        //立项时限
        SearchConditionItem lxsx = conditionFactory.createCondition(ConditionType.DATE,502327, new String[]{"lxsx", "lxsxfrom", "lxsxto"});
        lxsx.setLabel("立项时间");
        conditionItems.add(lxsx);

        addGroups.add(new SearchConditionGroup("查询条件",true,conditionItems));


        apidatas.put("condition",addGroups);

        return apidatas;

    }
    public JSONObject checkButton(Map<String,Object> params, User user){
        JSONObject json=new JSONObject();
        boolean flag=false;
        String rwid=Util.null2String(params.get("rwid")).trim() ;
        if(user==null||"".equals(Util.null2String(user.getUID()))||"".equals(rwid)){
            flag=false;
        }
        RecordSet re=new RecordSet();
        String DBType=re.getDBType();
        //log.writeLog("DBType"+DBType);

        String sql="select 1 from uf_dcsxzb where ISNULL(zt,-1) in (0,-1) and modedatacreater= "+user.getUID();
        re.execute(sql);
        if(re.next()){
            flag=true;
        }
        json.put("flag",flag);
        return json;
    }

}
